package org.lanqiao.sjx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Work;
import org.lanqiao.bean.Zuopin;
import org.lanqiao.sjx.dao.ZuopinDao;
import org.lanqiao.sjx.dao.impl.ZuopinImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/Zuopinservlet")
public class Zuopinservlet extends BaseServlet {
	ZuopinDao dao=new ZuopinImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String type = getString("type");
		redata.setMsg("");
		if (!isNull("type")) {
			switch (type) {
			case "select":
				selectZuopin();
				break;
			case "insert":
				insertZuopin();
				break;
			case "update":
				updateZuopin();
				break;
			case "delete":
				deleteZuopin();
				break;
				default:
					break;
			}		
		}
		else {
			redata.setCode(3);
			redata.setData(null);

		}
		System.out.println(redata);
		response.getWriter().write(new Gson().toJson(redata));
	}

	private void deleteZuopin() {
		// TODO Auto-generated method stub
		
		
	}

	private void updateZuopin() {
		System.out.println("update");
		Integer zid=getInt("id");
		String herf = getString("herf");
		String zpDescription = getString("zpDescription");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i = dao.updateZuopin(new Zuopin(herf, zpDescription,zid));
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("修改成功");
		}
		
		
	}

	private void insertZuopin() {
		System.out.println("insertzuopin");
		Integer userId = getInt("userId");
		String herf = getString("herf");
		String zpDescription = getString("zpDescription");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i=dao.insertZuopin(new Zuopin(userId, herf, zpDescription));
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("添加成功");
		}
		
		
	}

	private void selectZuopin() {
		System.out.println("select查询");
		Integer userId=getInt("userId");
		System.out.println(userId);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Zuopin> selectWork = dao.selectZuopin(userId);
		redata.setCode(2);
		redata.setData(selectWork);
		redata.setMsg("查询成功");
	
	}
}
